package com.miage.m1.xml.project.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import com.miage.m1.xml.interfaces.iClubRepository;
import com.miage.m1.xml.project.Club;
import com.miage.m1.xml.project.ClubReader;
import com.miage.m1.xml.project.ClubSource;
import com.miage.m1.xml.test.ClubRepositoryTest;


/**
 * Servlet implementation class ClubInfoServlet
 */

public class ClubInfoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private iClubRepository clubRepository;
	private String xslt;
	private final static String PRESIDENT_XSLT_NAME = "presidentStylesheetXslt";
	private final static String COACH_XSLT_NAME = "coachStylesheetXslt";
	private final static String DEFAULT_XSLT_NAME = "defaultStylesheetXslt";
	
	/* Getters and Setters */
	public iClubRepository getClubRepository(){ return this.clubRepository; }
	
	public String getXslt(){ return this.xslt; } 
	public void setXslt(String xslt) { this.xslt = xslt; }
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ClubInfoServlet(iClubRepository repository) {
        super();
        // TODO Auto-generated constructor stub
        
        this.clubRepository = repository;
    }
    
    public ClubInfoServlet() {
        this(new ClubRepositoryTest());
        // TODO Auto-generated constructor stub
    } 

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		session.setAttribute("userType", request.getParameter("userType"));
		
		processRequest(request,response);
	}
	
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request,response);
	}

	/* Methods and Properties */
	
	/** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException {
		try {
			HttpSession session = request.getSession();
			
			if(session.getAttribute("userType").equals("president"))
				this.setXslt(PRESIDENT_XSLT_NAME);
			else if(session.getAttribute("userType").equals("coach"))
				this.setXslt(COACH_XSLT_NAME);
			else
				this.setXslt(DEFAULT_XSLT_NAME);
				
			
			response.setContentType("text/html");
			PrintWriter out = response.getWriter();
			
			List<Club> clubs = this.getClubRepository().find();
			
			XMLReader clubReader = new ClubReader();
		    InputSource clubSource = new ClubSource(clubs); 
		    Source source = new SAXSource(clubReader, clubSource);
		    
		    TransformerFactory tFactory = TransformerFactory.newInstance();
		    ServletContext webApp = this.getServletContext();
		    Result result = new StreamResult(out);
		    
		    String filePath = webApp.getRealPath(this.getInitParameter(this.getXslt()));
			Transformer transformer = tFactory.newTransformer(new StreamSource(filePath));
			transformer.setParameter("rootPath", request.getContextPath());
			transformer.setOutputProperty(OutputKeys.INDENT, "yes");
			transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
		       
		    // Transformation
		    transformer.transform(source, result);
		    
		    out.close();
		    
		}catch(Exception e){
        	e.printStackTrace();	
        }	
    	
    }

}
